#include "stack.h"

void stack_create(stack **S, int len)
{
    (*S) = (stack *)malloc(sizeof(stack));
    (*S)->data = (int *)malloc(sizeof(int) * len);

    (*S)->top = -1;
    (*S)->max = len;
}

void stack_push(stack *S, int data)
{
    if (NULL == S) {
        printf("无效栈\n");
        return ;
    }

    S->top++;
    S->data[S->top] = data;
}

int stack_pop(stack *S)
{
    if (NULL == S) {
        printf("无效栈.\n");
        return -1;
    }

    if (is_empty(S)) {
        printf("空栈\n");
        return -1;
    }

    S->top--;
    return S->data[S->top + 1];
}

int is_empty(stack *S)
{
    if (NULL == S) {
        printf("无效栈\n");
        return -1;
    }

    return S->top == -1 ? 1 : 0;
}

int is_full(stack *S)
{
    if (NULL == S) {
        printf("无效栈\n");
        return -1;
    }

    return S->top == S->max - 1 ? 1 : 0;
}

